package com.ball.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ball.dao.LogMapper;
import com.ball.entity.Log;
import com.ball.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class LogServiceImpl implements LogService {
    @Autowired
    LogMapper logMapper;

    @Override
    public String add(JSONObject json) {
//        int id=Integer.parseInt(json.getString("id"));
        String username = json.getString("username");
        String log=json.getString("log");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time=df.format(new Date());// new Date()为获取当前系统时间
        Log l=new Log(username,log,time);
        logMapper.add(l);
        return "add log: "+l.toString();
    }

    @Override
    public String update(JSONObject json) {
        int id=Integer.parseInt(json.getString("id"));
        String username = json.getString("username");
        String log=json.getString("log");
        String time=json.getString("time");
        Log l=new Log(id,username,log,time);
        logMapper.update(l);
        return "update log: "+l.toString();
    }

    @Override
    public String deleteLogById(JSONObject json) {
        int id=Integer.parseInt(json.getString("id"));
        logMapper.deleteLogById(id);
        return "deleteLogById: "+id;
    }

    @Override
    public String deleteLogByName(JSONObject json) {
        String username = json.getString("username");
        logMapper.deleteLogByName(username);
        return "deleteLogById: "+username;
    }

    @Override
    public Log queryLogById(JSONObject json) {
        int id=Integer.parseInt(json.getString("id"));
        return logMapper.queryLogById(id);
    }

    @Override
    public List<Log> queryLogList(JSONObject json) {
        return logMapper.queryLogList();
    }

    @Override
    public List<Log> queryLogByUsername(JSONObject json) {
        String username = json.getString("username");
        return logMapper.queryLogByUsername(username);
    }
}
